//
//  KZShadowButton.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/8/22.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZShadowButton.h"


@implementation KZShadowButton
{
    UIView *_gradientView;
}
- (instancetype)init:(void (^)(UILabel *))config {
    
    if (self = [super init]) {
        
        _gradientView = [UIView new];
        _gradientView.userInteractionEnabled = false;
        _gradientView.layer.masksToBounds = true;
        [self addSubview:_gradientView];
        [_gradientView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        [gradientLayer setStartPoint:CGPointMake(0, 1)];
        [gradientLayer setEndPoint:CGPointMake(1, 1)];
        [_gradientView.layer insertSublayer:gradientLayer atIndex:0];

        _label = [[UILabel alloc] init];
        _label.adjustsFontSizeToFitWidth = true;
        _label.text = @"";
        _label.font = FONT(14);
        _label.textAlignment = NSTextAlignmentCenter;
        _label.textColor = [UIColor whiteColor];
        [self addSubview:_label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        if (config) {
            config(self.label);
        }
    }
    return self;
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    if (_colors == nil) {
        _colors = @[
                    (__bridge id)rgba(11, 175, 251, 1).CGColor,
                    (__bridge id)rgba(71, 204, 255, 1).CGColor,
                    ];
    }
    
    
    self.layer.shadowColor = (__bridge CGColorRef)_colors.firstObject;
    self.layer.shadowRadius = 6;
    self.layer.shadowOpacity = 0.6;
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(5, 10)];
    [path addLineToPoint:CGPointMake(10, self.height + 5)];
    [path addLineToPoint:CGPointMake(self.width - 10, self.height + 5)];
    [path addLineToPoint:CGPointMake(self.width - 5, 10)];
    [path closePath];
    self.layer.shadowPath = path.CGPath;
    
    _gradientView.layer.cornerRadius = self.height / 2;
    
    CAGradientLayer *gradientLayer = (CAGradientLayer *)_gradientView.layer.sublayers.firstObject;
    gradientLayer.bounds = CGRectMake(0, 0, self.width, self.height);
    gradientLayer.position = CGPointMake(self.width / 2, self.height / 2);
    gradientLayer.colors = _colors;
}



@end
